package com.gblfy.ws.client;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.rmi.RemoteException;

/**
 * Axis客户端
 *
 * @author guobin
 * @date 2021-09-17
 */
@Component
public class AxisClient {
    private final static Logger log = LoggerFactory.getLogger(AxisClient.class);

    public static void main(String[] args) throws Exception {
        String axisUrl = "http://localhost:8080/services/axisServiceShell?wsdl";
        String namespaceURI = "http://localhost:8080/services/axisServiceShell";
        // String method = "sayHello";
        String method = "sayHello2";
        String reqXml = "1";
        String reqXml2 = "2";

        //调用axis服务
        // AxisClient.axisSendMsg(axisUrl, namespaceURI, method, reqXml);
        AxisClient.axisSendMsg(axisUrl, namespaceURI, method, reqXml, reqXml2);
    }

    /**
     * @param url       WebService地址
     * @param namespace 命名空间
     * @param method    方法
     * @param tReqXml   请求报文
     * @return
     * @throws Exception
     */
    public static String axisSendMsg(String url, String namespace, String method, String tReqXml)
            throws Exception {
        Service service = new Service();
        Call call;
        String res = null;
        call = (Call) service.createCall();
        long forStrTime = 0L;

        if (StringUtils.isBlank(url)) {
            throw new RuntimeException("调用url地址等于空，请核实地址是否正确！");
        }
        if (StringUtils.isBlank(namespace)) {
            throw new RuntimeException("调用namespace等于空，请核实命名空间是否正确！");
        }
        if (StringUtils.isBlank(method)) {
            throw new RuntimeException("调用method等于空，请核实方法名是否正确！");
        }
        if (ObjectUtils.isEmpty(tReqXml)) {
            throw new RuntimeException("调发送报文等于空，请核实发送内容是否正确！");
        }

        call.setTargetEndpointAddress(new java.net.URL(url));

        //特殊处理部分    start
        String subUrl = url.substring(url.lastIndexOf("/"));
        log.info("转发路径标志 {}", subUrl.substring(1, subUrl.length()));

        //针对xxx需求添加单独逻辑判断
        if ("ws_fxhx_ws".equals(subUrl.substring(1, subUrl.length()))) {
            call.addParameter("xmlStr", org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);
            call.setReturnType(org.apache.axis.Constants.XSD_STRING);
        }

        //特殊处理部分    end
        call.setOperationName(new QName(namespace, method));// 这是要调用的方法
        log.info("开始转发请求报文");
        forStrTime = System.currentTimeMillis();
        log.info("开始转发时间: {}-毫秒", forStrTime);

        try {
            res = (String) call.invoke(new Object[]{tReqXml});
        } catch (RemoteException e) {
            e.printStackTrace();
            throw e;
        }

        long forEndTime = System.currentTimeMillis();
        log.info("转发结束时间: {}-毫秒", forEndTime);
        long endToStart = (long) (forEndTime - forStrTime);
        log.info("转发消耗的时间:: {}-毫秒", endToStart);
        log.info("响应报文: {}", res);

        return res;
    }

    public static String axisSendMsg(String url, String namespace, String method, String tReqXml, String tReqXml2)
            throws Exception {
        Service service = new Service();
        Call call;
        String res = null;
        call = (Call) service.createCall();
        long forStrTime = 0L;

        if (StringUtils.isBlank(url)) {
            throw new RuntimeException("调用url地址等于空，请核实地址是否正确！");
        }
        if (StringUtils.isBlank(namespace)) {
            throw new RuntimeException("调用namespace等于空，请核实命名空间是否正确！");
        }
        if (StringUtils.isBlank(method)) {
            throw new RuntimeException("调用method等于空，请核实方法名是否正确！");
        }
        if (ObjectUtils.isEmpty(tReqXml)) {
            throw new RuntimeException("调发送报文等于空，请核实发送内容是否正确！");
        }

        call.setTargetEndpointAddress(new java.net.URL(url));

        //特殊处理部分    start
        String subUrl = url.substring(url.lastIndexOf("/"));
        log.info("转发路径标志 {}", subUrl.substring(1, subUrl.length()));

        //针对xxx需求添加单独逻辑判断
        if ("ws_fxhx_ws".equals(subUrl.substring(1, subUrl.length()))) {
            call.addParameter("xmlStr", org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);
            call.setReturnType(org.apache.axis.Constants.XSD_STRING);
        }

        //特殊处理部分    end
        call.setOperationName(new QName(namespace, method));// 这是要调用的方法
        log.info("开始转发请求报文");
        forStrTime = System.currentTimeMillis();
        log.info("开始转发时间: {}-毫秒", forStrTime);

        try {
            res = (String) call.invoke(new Object[]{tReqXml, tReqXml2});
        } catch (RemoteException e) {
            e.printStackTrace();
            throw e;
        }

        long forEndTime = System.currentTimeMillis();
        log.info("转发结束时间: {}-毫秒", forEndTime);
        long endToStart = (long) (forEndTime - forStrTime);
        log.info("转发消耗的时间:: {}-毫秒", endToStart);
        log.info("响应报文: {}", res);

        return res;
    }
}
